﻿using System;
using System.Collections.Generic;
using System.Text;

namespace LdCms.Plugins.Map.Amap
{
    using LdCms.Common.Json;
    using LdCms.Common.Logs;
    using LdCms.Common.Net;
    using LdCms.Common.Security;
    using LdCms.Common.Web;
    using LdCms.Plugins.Map.Amap.Common;
    using LdCms.Plugins.Map.Amap.Models;

    public partial class AmapMapHelper: WebServiceAPIUrl
    {
        public string Key { get; set; }
        public string PrivateKey { get; set; }
        public AmapMapHelper() { }
        public AmapMapHelper(string key, string privateKey)
        {
            this.Key = key;
            this.PrivateKey = privateKey;
        }
        /// <summary>
        /// GET请求
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        private string GetUrl(string url)
        {
            try
            {
                string str = GeneralCodeHelper.GetRandomInt(8);
                string result = HttpHelper.GetToUrl(url);
                LogsManager.LogPath = string.Empty;
                LogsManager.logFielPrefix = string.Empty;
                LogsManager.WriteLog(LogsFile.Debug, string.Format("[{0}]-高德地图WebServiceAPI--开结--", str));
                LogsManager.WriteLog(LogsFile.Debug, string.Format("[{0}]-IP：{1}", str, Net.Ip));
                LogsManager.WriteLog(LogsFile.Debug, string.Format("[{0}]-地址：{1}", str, url));
                LogsManager.WriteLog(LogsFile.Debug, string.Format("[{0}]-结果：{1}", str, result.MiniClear()));
                LogsManager.WriteLog(LogsFile.Debug, string.Format("[{0}]-高德地图WebServiceAPI--结束--", str));
                return result;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }


        #region 地理/逆地理编码
        public string CreateGeocodeGeoUrl(GeocodeGeo geocodeGeo)
        {
            try
            {
                //placeSearch.key = Key;
                string url = geocodeGeoUrl;
                string path = new Uri(url).AbsolutePath;
                var sdParams = Utilities.EntityToSortedDictionary(geocodeGeo);
                string sign = Utilities.GetSign(sdParams, path, PrivateKey);
                string urlParams = Utilities.SortedDictionaryToUrlParams(sdParams);
                return string.Format("{0}?{1}&sig={2}", url, urlParams, sign);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        public string GetGeocodeGeo(GeocodeGeo geocodeGeo)
        {
            try
            {
                string url = CreateGeocodeGeoUrl(geocodeGeo);
                return GetUrl(url);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        public GeocodeGeoResult GetGeocodeGeoPro(string url)
        {
            try
            {
                string result = GetUrl(url);
                return result.ToObject<GeocodeGeoResult>();
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        public GeocodeGeoResult GetGeocodeGeoPro(GeocodeGeo geocodeGeo)
        {
            try
            {
                string result = GetGeocodeGeo(geocodeGeo);
                return result.ToObject<GeocodeGeoResult>();
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }

        public string CreateGeocodeRegeoUrl(GeocodeRegeo geocodeRegeo)
        {
            try
            {
                //placeSearch.key = Key;
                string url = geocodeRegeoUrl;
                string path = new Uri(url).AbsolutePath;
                var sdParams = Utilities.EntityToSortedDictionary(geocodeRegeo);
                string sign = Utilities.GetSign(sdParams, path, PrivateKey);
                string urlParams = Utilities.SortedDictionaryToUrlParams(sdParams);
                return string.Format("{0}?{1}&sig={2}", url, urlParams, sign);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        public string GetGeocodeRegeo(GeocodeRegeo geocodeRegeo)
        {
            try
            {
                string url = CreateGeocodeRegeoUrl(geocodeRegeo);
                return GetUrl(url);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }


        #endregion

        #region 路径规划 
        public string CreateDirectionWalkingUrl(DirectionWalking directionWalking)
        {
            try
            {
                string url = directionWalkingUrl;
                string path = new Uri(url).AbsolutePath;
                var sdParams = Utilities.EntityToSortedDictionary(directionWalking);
                string sign = Utilities.GetSign(sdParams, path, PrivateKey);
                string urlParams = Utilities.SortedDictionaryToUrlParams(sdParams);
                return string.Format("{0}?{1}&sig={2}", url, urlParams, sign);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        public string GetDirectionWalking(DirectionWalking directionWalking)
        {
            try
            {
                string url = CreateDirectionWalkingUrl(directionWalking);
                return GetUrl(url);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }

        public string CreateDirectionTransitUrl(DirectionTransit directionTransit)
        {
            try
            {
                string url = directionTransitUrl;
                string path = new Uri(url).AbsolutePath;
                var sdParams = Utilities.EntityToSortedDictionary(directionTransit);
                string sign = Utilities.GetSign(sdParams, path, PrivateKey);
                string urlParams = Utilities.SortedDictionaryToUrlParams(sdParams);
                return string.Format("{0}?{1}&sig={2}", url, urlParams, sign);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        public string GetDirectionTransit(DirectionTransit directionTransit)
        {
            try
            {
                string url = CreateDirectionTransitUrl(directionTransit);
                return GetUrl(url);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }

        public string CreateDirectionDrivingUrl(DirectionDriving directionDriving)
        {
            try
            {
                string url = directionDrivingUrl;
                string path = new Uri(url).AbsolutePath;
                var sdParams = Utilities.EntityToSortedDictionary(directionDriving);
                string sign = Utilities.GetSign(sdParams, path, PrivateKey);
                string urlParams = Utilities.SortedDictionaryToUrlParams(sdParams);
                return string.Format("{0}?{1}&sig={2}", url, urlParams, sign);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        public string GetDirectionDriving(DirectionDriving directionDriving)
        {
            try
            {
                string url = CreateDirectionDrivingUrl(directionDriving);
                return GetUrl(url);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }

        public string CreateDirectionBicyclingUrl(DirectionBicycling directionBicycling)
        {
            try
            {
                string url = directionBicyclingUrl;
                string path = new Uri(url).AbsolutePath;
                var sdParams = Utilities.EntityToSortedDictionary(directionBicycling);
                string sign = Utilities.GetSign(sdParams, path, PrivateKey);
                string urlParams = Utilities.SortedDictionaryToUrlParams(sdParams);
                return string.Format("{0}?{1}&sig={2}", url, urlParams, sign);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        public string GetDirectionBicycling(DirectionBicycling directionBicycling)
        {
            try
            {
                string url = CreateDirectionBicyclingUrl(directionBicycling);
                return GetUrl(url);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }

        public string CreateDirectionTruckUrl(DirectionTruck directionTruck)
        {
            try
            {
                string url = directionTruckUrl;
                string path = new Uri(url).AbsolutePath;
                var sdParams = Utilities.EntityToSortedDictionary(directionTruck);
                string sign = Utilities.GetSign(sdParams, path, PrivateKey);
                string urlParams = Utilities.SortedDictionaryToUrlParams(sdParams);
                return string.Format("{0}?{1}&sig={2}", url, urlParams, sign);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        public string GetDirectionTruck(DirectionTruck directionTruck)
        {
            try
            {
                string url = CreateDirectionTruckUrl(directionTruck);
                return GetUrl(url);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }

        public string CreateDistanceUrl(Distance distance)
        {
            try
            {
                string url = distanceUrl;
                string path = new Uri(url).AbsolutePath;
                var sdParams = Utilities.EntityToSortedDictionary(distance);
                string sign = Utilities.GetSign(sdParams, path, PrivateKey);
                string urlParams = Utilities.SortedDictionaryToUrlParams(sdParams);
                return string.Format("{0}?{1}&sig={2}", url, urlParams, sign);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        public string GetDistance(Distance distance)
        {
            try
            {
                string url = CreateDistanceUrl(distance);
                return GetUrl(url);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        #endregion

        #region 行政区域查询 
        public string CreateDistrictUrl(District district)
        {
            try
            {
                string url = districtUrl;
                string path = new Uri(url).AbsolutePath;
                var sdParams = Utilities.EntityToSortedDictionary(district);
                string sign = Utilities.GetSign(sdParams, path, PrivateKey);
                string urlParams = Utilities.SortedDictionaryToUrlParams(sdParams);
                return string.Format("{0}?{1}&sig={2}", url, urlParams, sign);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        public string GetDistrict(District district)
        {
            try
            {
                string url = CreateDistrictUrl(district);
                return GetUrl(url);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        #endregion

        #region 搜索POI 
        public string CreatePlaceTextUrl(PlaceText placeText)
        {
            try
            {
                string url = placeTextUrl;
                string path = new Uri(url).AbsolutePath;
                var sdParams = Utilities.EntityToSortedDictionary(placeText);
                string sign = Utilities.GetSign(sdParams, path, PrivateKey);
                string urlParams = Utilities.SortedDictionaryToUrlParams(sdParams);
                return string.Format("{0}?{1}&sig={2}", url, urlParams, sign);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        public string GetPlaceText(PlaceText placeText)
        {
            try
            {
                string url = CreatePlaceTextUrl(placeText);
                return GetUrl(url);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }

        public string CreatePlaceAroundUrl(PlaceAround placeAround)
        {
            try
            {
                string url = placeAroundUrl;
                string path = new Uri(url).AbsolutePath;
                var sdParams = Utilities.EntityToSortedDictionary(placeAround);
                string sign = Utilities.GetSign(sdParams, path, PrivateKey);
                string urlParams = Utilities.SortedDictionaryToUrlParams(sdParams);
                return string.Format("{0}?{1}&sig={2}", url, urlParams, sign);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        public string GetPlaceAround(PlaceAround placeAround)
        {
            try
            {
                string url = CreatePlaceAroundUrl(placeAround);
                return GetUrl(url);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }

        public string CreatePlacePolygonUrl(PlacePolygon placePolygon)
        {
            try
            {
                string url = placePolygonUrl;
                string path = new Uri(url).AbsolutePath;
                var sdParams = Utilities.EntityToSortedDictionary(placePolygon);
                string sign = Utilities.GetSign(sdParams, path, PrivateKey);
                string urlParams = Utilities.SortedDictionaryToUrlParams(sdParams);
                return string.Format("{0}?{1}&sig={2}", url, urlParams, sign);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        public string GetPlacePolygon(PlacePolygon placePolygon)
        {
            try
            {
                string url = CreatePlacePolygonUrl(placePolygon);
                return GetUrl(url);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }

        public string CreatePlaceDetailUrl(PlaceDetail placeDetail)
        {
            try
            {
                string url = placeDetailUrl;
                string path = new Uri(url).AbsolutePath;
                var sdParams = Utilities.EntityToSortedDictionary(placeDetail);
                string sign = Utilities.GetSign(sdParams, path, PrivateKey);
                string urlParams = Utilities.SortedDictionaryToUrlParams(sdParams);
                return string.Format("{0}?{1}&sig={2}", url, urlParams, sign);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        public string GetPlaceDetail(PlaceDetail placeDetail)
        {
            try
            {
                string url = CreatePlaceDetailUrl(placeDetail);
                return GetUrl(url);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        #endregion

        #region IP定位 
        public string CreateIPUrl(IP ip)
        {
            try
            {
                string url = ipUrl;
                string path = new Uri(url).AbsolutePath;
                var sdParams = Utilities.EntityToSortedDictionary(ip);
                string sign = Utilities.GetSign(sdParams, path, PrivateKey);
                string urlParams = Utilities.SortedDictionaryToUrlParams(sdParams);
                return string.Format("{0}?{1}&sig={2}", url, urlParams, sign);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        public string GetIP(IP ip)
        {
            try
            {
                string url = CreateIPUrl(ip);
                return GetUrl(url);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        #endregion

        #region 静态地图
        public string CreateStaticmapUrl(Staticmap staticmap)
        {
            try
            {
                string url = staticmapUrl;
                string path = new Uri(url).AbsolutePath;
                var sdParams = Utilities.EntityToSortedDictionary(staticmap);
                string sign = Utilities.GetSign(sdParams, path, PrivateKey);
                string urlParams = Utilities.SortedDictionaryToUrlParams(sdParams);
                return string.Format("{0}?{1}&sig={2}", url, urlParams, sign);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        public string GetStaticmap(Staticmap staticmap)
        {
            try
            {
                string url = CreateStaticmapUrl(staticmap);
                return new { url }.ToJson();
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        #endregion

        #region 坐标转换
        public string CreateAssistantCoordinateConvertUrl(AssistantCoordinateConvert assistantCoordinateConvert)
        {
            try
            {
                string url = assistantCoordinateConvertUrl;
                string path = new Uri(url).AbsolutePath;
                var sdParams = Utilities.EntityToSortedDictionary(assistantCoordinateConvert);
                string sign = Utilities.GetSign(sdParams, path, PrivateKey);
                string urlParams = Utilities.SortedDictionaryToUrlParams(sdParams);
                return string.Format("{0}?{1}&sig={2}", url, urlParams, sign);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        public string GetAssistantCoordinateConvert(AssistantCoordinateConvert assistantCoordinateConvert)
        {
            try
            {
                string url = CreateAssistantCoordinateConvertUrl(assistantCoordinateConvert);
                return GetUrl(url);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        #endregion

        #region 天气查询
        public string CreateWeatherInfoUrl(WeatherInfo weatherInfo)
        {
            try
            {
                string url = weatherInfoUrl;
                string path = new Uri(url).AbsolutePath;
                var sdParams = Utilities.EntityToSortedDictionary(weatherInfo);
                string sign = Utilities.GetSign(sdParams, path, PrivateKey);
                string urlParams = Utilities.SortedDictionaryToUrlParams(sdParams);
                return string.Format("{0}?{1}&sig={2}", url, urlParams, sign);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        public string GetWeatherInfo(WeatherInfo weatherInfo)
        {
            try
            {
                string url = CreateWeatherInfoUrl(weatherInfo);
                return GetUrl(url);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        #endregion

        #region 输入提示
        public string CreateAssistantInputtipsUrl(AssistantInputtips assistantInputtips)
        {
            try
            {
                string url = assistantInputtipsUrl;
                string path = new Uri(url).AbsolutePath;
                var sdParams = Utilities.EntityToSortedDictionary(assistantInputtips);
                string sign = Utilities.GetSign(sdParams, path, PrivateKey);
                string urlParams = Utilities.SortedDictionaryToUrlParams(sdParams);
                return string.Format("{0}?{1}&sig={2}", url, urlParams, sign);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        public string GetAssistantInputtips(AssistantInputtips assistantInputtips)
        {
            try
            {
                string url = CreateAssistantInputtipsUrl(assistantInputtips);
                return GetUrl(url);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        #endregion

        #region 交通态势
        public string CreateTrafficStatusRectangleUrl(TrafficStatusRectangle trafficStatusRectangle)
        {
            try
            {
                string url = trafficStatusRectangleUrl;
                string path = new Uri(url).AbsolutePath;
                var sdParams = Utilities.EntityToSortedDictionary(trafficStatusRectangle);
                string sign = Utilities.GetSign(sdParams, path, PrivateKey);
                string urlParams = Utilities.SortedDictionaryToUrlParams(sdParams);
                return string.Format("{0}?{1}&sig={2}", url, urlParams, sign);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        public string GetTrafficStatusRectangle(TrafficStatusRectangle trafficStatusRectangle)
        {
            try
            {
                string url = CreateTrafficStatusRectangleUrl(trafficStatusRectangle);
                return GetUrl(url);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }

        public string CreateTrafficStatusCircleUrl(TrafficStatusCircle trafficStatusCircle)
        {
            try
            {
                string url = trafficStatusCircleUrl;
                string path = new Uri(url).AbsolutePath;
                var sdParams = Utilities.EntityToSortedDictionary(trafficStatusCircle);
                string sign = Utilities.GetSign(sdParams, path, PrivateKey);
                string urlParams = Utilities.SortedDictionaryToUrlParams(sdParams);
                return string.Format("{0}?{1}&sig={2}", url, urlParams, sign);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        public string GetTrafficStatusCircle(TrafficStatusCircle trafficStatusCircle)
        {
            try
            {
                string url = CreateTrafficStatusCircleUrl(trafficStatusCircle);
                return GetUrl(url);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }

        public string CreateTrafficStatusRoadUrl(TrafficStatusRoad trafficStatusRoad)
        {
            try
            {
                string url = trafficStatusRoadUrl;
                string path = new Uri(url).AbsolutePath;
                var sdParams = Utilities.EntityToSortedDictionary(trafficStatusRoad);
                string sign = Utilities.GetSign(sdParams, path, PrivateKey);
                string urlParams = Utilities.SortedDictionaryToUrlParams(sdParams);
                return string.Format("{0}?{1}&sig={2}", url, urlParams, sign);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        public string GetTrafficStatusRoad(TrafficStatusRoad trafficStatusRoad)
        {
            try
            {
                string url = CreateTrafficStatusRoadUrl(trafficStatusRoad);
                return GetUrl(url);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        #endregion


    }
}
